IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'spCdsNCreateAdmEmailAddress')
	BEGIN
		PRINT 'Dropping Procedure spCdsNCreateAdmEmailAddress'
		DROP  Procedure  spCdsNCreateAdmEmailAddress    
	END

GO

PRINT 'Creating Procedure spCdsNCreateAdmEmailAddress'
GO
/******************************************************************************
**		VSS File: $/spCdsNCreateAdmEmailAddress.sql 
**		Name: spCdsNCreateAdmEmailAddress    
**		Desc: 	This procedure creates a new record in
**				the AdmEmailAddress table.
**              
**		Return values: None
** 
**		Called by:   
**              
*******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:				Description:
**		--------		--------				-------------------------------------------
**    
*******************************************************************************/
CREATE Procedure spCdsNCreateAdmEmailAddress (
	/* Param List */
	@emailAddress			VARCHAR(100),
	@admPersonId			INT,
	@admEmailAddressTypeId	INT,
	@description			VARCHAR(128)
)	   
AS    
	SET NOCOUNT ON

	DECLARE @_maxAdmEmailId				INT
	DECLARE @_startingId				INT
	DECLARE @_errorCode					INT
	
	SET @_startingId = 1
	SET @_errorCode  = 0
		
	-- Setup the IDs to be inserted by selecting the max
	-- Id of the column, and if null, start at 1.
	SET @_maxAdmEmailId = COALESCE (((SELECT MAX(Id) from AdmEmailAddress)+1), 1)
	
	-- Transaction.
	BEGIN TRANSACTION spCdsNCreateAdmEmailAddress

		-- Insert the AdmEmailAddress
		INSERT INTO AdmEmailAddress ( Id, 
								EmailAddress, 
								AdmPersonId,
								AdmEmailAddressTypeId, 
								Description) 
		VALUES
							 (	@_maxAdmEmailId,
								@emailAddress,
								@admPersonId,
								@admEmailAddressTypeId,
								@description)		
	
		SET @_errorCode = @@ERROR
	
	    IF @_errorCode <> 0 BEGIN
            ROLLBACK TRANSACTION spCdsNCreateAdmEmailAddress
            RAISERROR('Unable to create AdmEmailAddress record for email: %s', 16, 1, @admPersonId)
            RETURN @_errorCode
        END

		
		-- COMMIT THE CHANGES.
		COMMIT TRANSACTION spCdsNCreateAdmEmailAddress

	
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
  